package cn.joeg.app.kernel.service;

import cn.joeg.app.css.util.DBHelper;
import cn.joeg.app.kernel.db.util.DBInput;
import cn.joeg.app.kernel.db.util.DBInvoker;
import cn.joeg.app.kernel.db.util.DBOutput;
import com.ecfront.dew.common.Resp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.lang.reflect.InvocationTargetException;
import java.math.BigInteger;
import java.util.List;

@Service
public class LoginService extends DBHelper implements DBInvoker<LoginService.Input, LoginService.Output> {

    @Autowired
    @Qualifier("entityManagerFactory")
    private EntityManager em;


    @Override
    public Resp<Output> execute(Input input) {


        String loginSql = "select * from  users  where username  = :username";
        Query query = em.createNativeQuery(loginSql);
        query.setParameter("username", input.getLoginId());

        List<LoginVerifyDTO> dtos = getResultList(query, LoginVerifyDTO.class);

        if (dtos == null || dtos.isEmpty()) {
            return Resp.notFound("该登录名对应用户不存在");
        }
        LoginVerifyDTO dto = dtos.get(0);
        String encryptPwd = input.getPassword();
        if (!encryptPwd.equals(dto.getPassword())) {
            return Resp.badRequest("密码错误");
        }

        return Resp.success(new Output());
    }

    public static class Input extends DBInput {
        private String loginId;
        private String password;
        private String sid;

        public String getLoginId() {
            return loginId;
        }

        public void setLoginId(String loginId) {
            this.loginId = loginId;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public String getSid() {
            return sid;
        }

        public void setSid(String sid) {
            this.sid = sid;
        }
    }


    public static class Output extends DBOutput {


    }


    public static class LoginVerifyDTO {

        private BigInteger id;
        private String username;
        private String password;
        private String password_salt;

        public LoginVerifyDTO(BigInteger id, String username, String password, String password_salt) {
            this.id = id;
            this.username = username;
            this.password = password;
            this.password_salt = password_salt;
        }

        public BigInteger getId() {
            return id;
        }

        public void setId(BigInteger id) {
            this.id = id;
        }

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public String getPassword_salt() {
            return password_salt;
        }

        public void setPassword_salt(String password_salt) {
            this.password_salt = password_salt;
        }
    }


}
